// pages/index/index.js

import "../../utils/ValidateUtils"
import { array_exist } from "../../utils/ValidateUtils"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 搜索内容
    searchValue: "",
    // 当前查看的钱包
    currentWallet: null,
    // 当前查看的用户
    currentUser: null,
    // 当前搜索到的用户
    currentSearchUsers: null,

    // 已选钱包
    currentWallets: [],
    users: [
      {
        name: "Akihi", avatar: "/images/avatar1.png", wallets: [
          { id: 1, name: "红色" },
          { id: 2, name: "蓝色" },
          { id: 3, name: "黑色" },
          { id: 4, name: "白色" }
        ]
      },
      {
        name: "Akihi2", avatar: "/images/avatar1.png", wallets: [
          { id: 1, name: "红色" },
          { id: 2, name: "蓝色" },
          { id: 3, name: "黑色" },
          { id: 4, name: "白色" }
        ]
      },
      {
        name: "Akihi3", avatar: "/images/avatar1.png", wallets: [
          { id: 1, name: "红色" },
          { id: 2, name: "蓝色" },
          { id: 3, name: "黑色" },
          { id: 4, name: "白色" }
        ]
      },
      {
        name: "Akihi4", avatar: "/images/avatar1.png", wallets: [
          { id: 1, name: "红色" },
          { id: 2, name: "蓝色" },
          { id: 3, name: "黑色" },
          { id: 4, name: "白色" }
        ]
      },
      {
        name: "Akihi5", avatar: "/images/avatar1.png", wallets: [
          { id: 1, name: "红色" },
          { id: 2, name: "蓝色" },
          { id: 3, name: "黑色" },
          { id: 4, name: "白色" }
        ]
      },
      { name: "看看人家阿磊", avatar: "/images/avatar2.png" },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    this.data.aaa
  },

  /**
   * 显示钱包
   */
  showWallet(e) {
    let currentWallet = e.currentTarget.dataset.wallet
    let user = e.currentTarget.dataset.user

    this.setData({
      currentWallet: currentWallet,
      currentUser: user
    })
  },


  /**
   * 搜索事件
   */
  search: function(s) {
    this.data.currentSearchUsers = []
    let search = this.data.searchValue
    let usersNew = []
    array_exist(this.data.users).forEach(user => {
      user.name.indexOf(search) >= 0 && usersNew.push(user)
    })
    console.log('搜索结果：', this.data.currentSearchUsers)
    this.setData({
      currentSearchUsers: usersNew
    })
  },
  
  searchInput: function(e) {
    this.setData({
      searchValue: e.detail.value
    })
  }

})